package com.wang.back.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.common.pojo.BasePojoEnum;
import com.wang.common.pojo.BaseQuery;
import com.wang.common.util.ConstatFinalUtil;
import com.wang.common.web.controller.BaseController;
import com.wang.users.pojo.AtCar;
import com.wang.users.pojo.AtCarQuery;
import com.wang.users.pojo.AtIntersection;
import com.wang.users.pojo.AtIntersectionQuery;
import com.wang.users.pojo.AtRoute;
import com.wang.users.pojo.AtRouteQuery;
import com.wang.users.pojo.AtStat;
import com.wang.users.pojo.AtStatQuery;
import com.wang.users.service.AtCarService;
import com.wang.users.service.AtIntersectionService;
import com.wang.users.service.AtRouteService;
import com.wang.users.service.AtStatService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 示例的Controller
 * @author ZjxMi
 */
@Controller
@RequestMapping("/back/traffic")
@Log4j2
public class TrafficLightBackController extends BaseController {
    @Autowired
    private AtCarService carService;
    @Autowired
    private AtIntersectionService intersectionService;
    @Autowired
    private AtRouteService routeService;
    @Autowired
    private AtStatService statService;

    /**
     * 路口查询多条
     * @return
     */
    @RequestMapping("/intersectionList")
    public String intersectionList(AtIntersectionQuery paramQuery, Model model){
        log.info("==路口列表==");
        /* 分页对象 */
        IPage<AtIntersection> paramPage = null;
        try {
            paramPage = new Page<>(paramQuery.getCurrent(), paramQuery.getSize());
        } catch (Exception e) {
            paramPage = new Page<>(1, 20);
        }
        /* 时间处理 */
        if (StringUtils.isNotEmpty(paramQuery.getStDate())) {
            paramQuery.setPubTimeSt(this.dateUtil.strToDateTime(paramQuery.getStDate()));
        }
        if (StringUtils.isNotEmpty(paramQuery.getEdDate())) {
            paramQuery.setPubTimeEd(this.dateUtil.strToDateTime(paramQuery.getEdDate()));
        }
        paramQuery.addLike();
        /* 查询对象 */
        List<AtIntersection> dataList = this.intersectionService.findList(paramPage, paramQuery);
        paramQuery.removeLike();
        /* 列表数据 */
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_DATALIST, dataList);
        /* 分页数据 */
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PAGEINFOUTIL, paramPage);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PARAMQUERY, paramQuery);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ENUMSONE, new AtIntersection());
        /* 查询数据 */
        return "/back/traffic/intersectionList";
    }

    /**
     * 路口打开添加页面
     * @return
     */
    @RequestMapping("/intersectionInsert")
    public String intersectionInsert(AtIntersectionQuery paramQuery, Model model){
        log.info("==路口添加==");
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ONE, new AtIntersection());
        /* 查询数据 */
        return "/back/traffic/intersectionInsert";
    }

    /**
     * 路口添加提交操作
     * @return
     */
    @RequestMapping("/intersectionInsertSubmit")
    @ResponseBody
    public String intersectionInsertSubmit(AtIntersectionQuery paramQuery, Model model){
        log.info("==路口添加提交==");
        boolean dbFlag = this.intersectionService.save(paramQuery.getEntity());
        JSONObject resultJson = this.proccedResponseJson(dbFlag);
        /* 返回json给客户端 */
        return resultJson.toJSONString();
    }

    /**
     * 路口打开更新页面
     * 更新和详情是同一个页面;
     * @return
     */
    @RequestMapping("/intersectionUpdate")
    public String intersectionUpdate(AtIntersectionQuery paramQuery, Model model){
        log.info("==路口更新==");
        /* 根据id查询 */
        AtIntersection one = this.intersectionService.findOne(paramQuery);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ONE, one);

        if (BaseQuery.FINAL_OPER_UPDATE.equalsIgnoreCase(paramQuery.getOperType())) {
            /* 查询数据 */
            return "/back/traffic/intersectionUpdate";
        }
        /* 查询数据 */
        return "/back/traffic/intersectionInfo";
    }

    /**
     * 路口更新提交操作
     * <pre>
     * 路口表有10个字段,用户填写的只有5个字段;
     * 更新的时候只更新5个字段;剩下的5个字段不用更新
     * 千万不要把页面中传的参数SpringMVC会自动创建对象,这个对象不要直接更新数据库,很危险;
     * </pre>
     * @return
     */
    @RequestMapping("/intersectionUpdateSubmit")
    @ResponseBody
    public String intersectionUpdateSubmit(AtIntersectionQuery paramQuery, Model model){
        log.info("==路口更新提交==");
        AtIntersection entity = paramQuery.getEntity();
        if (StringUtils.isNotEmpty(paramQuery.getPubTime())) {
            entity.setPubTime(this.dateUtil.strToDateTime(paramQuery.getPubTime()));
        }
        boolean dbFlag = this.intersectionService.updateById(entity);
        JSONObject resultJson = this.proccedResponseJson(dbFlag);
        /* 返回json给客户端 */
        return resultJson.toJSONString();
    }

    /**
     * 路口打开添加页面
     * @return
     */
    @RequestMapping("/intersectionBatch")
    @ResponseBody
    public String intersectionBatch(AtIntersectionQuery paramQuery, Model model){
        log.info("==路口批量提交==");
        String operType = paramQuery.getOperType();
        String[] ids = paramQuery.getIds();
        String info = ConstatFinalUtil.FINAL_OPER_SUCCESS;
        int totalCount = 0;
        int succedCount = 0;
        int failedCount = 0;
        if (StringUtils.isNotEmpty(operType)) {
            if (Objects.nonNull(ids) && ids.length > 0) {
                totalCount = ids.length;
                for (String idTemp : ids) {
                    try {
                        AtIntersectionQuery paramQueryTemp = new AtIntersectionQuery();
                        AtIntersection entity = paramQueryTemp.getEntity();
                        entity.setId(idTemp);
                        /* 根据id查询每一个 记录对象 */
                        AtIntersection dataDbResult = this.intersectionService.findOne(paramQueryTemp);
                        if (dataDbResult != null) {
                            boolean dbFlag = false;
                            if (BasePojoEnum.OPERTYPE_DELETE.getCode().equalsIgnoreCase(operType)) {
                                /* 删除 */
                                dbFlag = this.intersectionService.removeById(idTemp);
                            } else if (BasePojoEnum.OPERTYPE_RESTORE.getCode().equalsIgnoreCase(operType)) {
                                /* 还原,为什么这里面要传这么多参数,一切的一切就是为了在xml中拼装sql语句 */
                                paramQueryTemp = new AtIntersectionQuery();
                                entity = paramQueryTemp.getEntity();
                                entity.setDelFlag(BasePojoEnum.DELFLAG_NO.getCode());
                                entity.setId(idTemp);
                                paramQueryTemp.setOperType(BasePojoEnum.OPERTYPE_RESTORE.getCode());
                                dbFlag = this.intersectionService.updateBatch(paramQueryTemp) > 0 ;
                            } else if (BasePojoEnum.OPERTYPE_EMPTY.getCode().equalsIgnoreCase(operType)) {
                                /* 清空 */
                                paramQueryTemp = new AtIntersectionQuery();
                                entity = paramQueryTemp.getEntity();
                                entity.setId(idTemp);
                                dbFlag = this.intersectionService.deleteBatch(paramQueryTemp) > 0 ;
                            } else if (BasePojoEnum.OPERTYPE_UPDATESTATUS.getCode().equalsIgnoreCase(operType)) {
                                /* 批量修改状态 */
                                dataDbResult.setStatus(paramQuery.getEntity().getStatus());
                                dbFlag = this.intersectionService.updateById(dataDbResult);
                            }
                            if (dbFlag) {
                                succedCount ++;
                            } else {
                                failedCount ++;
                            }
                        }
                    } catch (Exception e) {
                        log.error("批量操作失败了,id:{}", idTemp, e);
                        failedCount ++;
                    }
                }
            } else {
                /* 未选择类型,ids */
                info = "ids为空";
            }
        } else {
            /* 操作类型未选择 */
            info = "操作类型未选择";
        }

        String messInfo = "批量操作日志:提示信息:${info}, 总条数:${totalCount},成功条数:${succedCount},失败条数:${failedCount}";
        Map<String,String> paramsMap = new HashMap<>(1);
        paramsMap.put("info", info);
        paramsMap.put("totalCount", String.valueOf(totalCount));
        paramsMap.put("succedCount", String.valueOf(succedCount));
        paramsMap.put("failedCount", String.valueOf(failedCount));
        messInfo = this.regexUtil.replaceOperator(messInfo, paramsMap);

        paramsMap = new HashMap<>(1);
        paramsMap.put(ConstatFinalUtil.FINAL_CONFIG_INFO_SUCCESS_PARAM1, messInfo);
        /* 查询数据 */
        JSONObject resultJson = this.proccedResponseJson(ConstatFinalUtil.FINAL_OPER_SUCCESS.equalsIgnoreCase(info), paramsMap);
        log.info("==结果:{}==", resultJson);
        return resultJson.toJSONString();
    }

    /**
     * 车辆查询多条
     * @return
     */
    @RequestMapping("/carList")
    public String carList(AtCarQuery paramQuery, Model model){
        log.info("==车辆列表==");
        /* 分页对象 */
        IPage<AtCar> paramPage = null;
        try {
            paramPage = new Page<>(paramQuery.getCurrent(), paramQuery.getSize());
        } catch (Exception e) {
            paramPage = new Page<>(1, 20);
        }
        /* 时间处理 */
        if (StringUtils.isNotEmpty(paramQuery.getStDate())) {
            paramQuery.setPubTimeSt(this.dateUtil.strToDateTime(paramQuery.getStDate()));
        }
        if (StringUtils.isNotEmpty(paramQuery.getEdDate())) {
            paramQuery.setPubTimeEd(this.dateUtil.strToDateTime(paramQuery.getEdDate()));
        }
        paramQuery.addLike();
        /* 查询对象 */
        List<AtCar> dataList = this.carService.findList(paramPage, paramQuery);
        paramQuery.removeLike();
        /* 列表数据 */
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_DATALIST, dataList);
        /* 分页数据 */
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PAGEINFOUTIL, paramPage);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PARAMQUERY, paramQuery);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ENUMSONE, new AtCar());
        /* 查询数据 */
        return "/back/traffic/carList";
    }

    /**
     * 车辆打开添加页面
     * @return
     */
    @RequestMapping("/carInsert")
    public String carInsert(AtCarQuery paramQuery, Model model){
        log.info("==车辆添加==");
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ONE, new AtCar());
        /* 查询数据 */
        return "/back/traffic/carInsert";
    }

    /**
     * 车辆添加提交操作
     * @return
     */
    @RequestMapping("/carInsertSubmit")
    @ResponseBody
    public String carInsertSubmit(AtCarQuery paramQuery, Model model){
        log.info("==车辆添加提交==");
        boolean dbFlag = this.carService.save(paramQuery.getEntity());
        JSONObject resultJson = this.proccedResponseJson(dbFlag);
        /* 返回json给客户端 */
        return resultJson.toJSONString();
    }

    /**
     * 车辆打开更新页面
     * 更新和详情是同一个页面;
     * @return
     */
    @RequestMapping("/carUpdate")
    public String carUpdate(AtCarQuery paramQuery, Model model){
        log.info("==车辆更新==");
        /* 根据id查询 */
        AtCar one = this.carService.findOne(paramQuery);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ONE, one);

        if (BaseQuery.FINAL_OPER_UPDATE.equalsIgnoreCase(paramQuery.getOperType())) {
            /* 查询数据 */
            return "/back/traffic/carUpdate";
        }
        /* 查询数据 */
        return "/back/traffic/carInfo";
    }

    /**
     * 车辆更新提交操作
     * <pre>
     * 车辆表有10个字段,用户填写的只有5个字段;
     * 更新的时候只更新5个字段;剩下的5个字段不用更新
     * 千万不要把页面中传的参数SpringMVC会自动创建对象,这个对象不要直接更新数据库,很危险;
     * </pre>
     * @return
     */
    @RequestMapping("/carUpdateSubmit")
    @ResponseBody
    public String carUpdateSubmit(AtCarQuery paramQuery, Model model){
        log.info("==车辆更新提交==");
        AtCar entity = paramQuery.getEntity();
        if (StringUtils.isNotEmpty(paramQuery.getPubTime())) {
            entity.setPubTime(this.dateUtil.strToDateTime(paramQuery.getPubTime()));
        }
        boolean dbFlag = this.carService.updateById(entity);
        JSONObject resultJson = this.proccedResponseJson(dbFlag);
        /* 返回json给客户端 */
        return resultJson.toJSONString();
    }

    /**
     * 车辆打开添加页面
     * @return
     */
    @RequestMapping("/carBatch")
    @ResponseBody
    public String carBatch(AtCarQuery paramQuery, Model model){
        log.info("==车辆批量提交==");
        String operType = paramQuery.getOperType();
        String[] ids = paramQuery.getIds();
        String info = ConstatFinalUtil.FINAL_OPER_SUCCESS;
        int totalCount = 0;
        int succedCount = 0;
        int failedCount = 0;
        if (StringUtils.isNotEmpty(operType)) {
            if (Objects.nonNull(ids) && ids.length > 0) {
                totalCount = ids.length;
                for (String idTemp : ids) {
                    try {
                        AtCarQuery paramQueryTemp = new AtCarQuery();
                        AtCar entity = paramQueryTemp.getEntity();
                        entity.setId(idTemp);
                        /* 根据id查询每一个 记录对象 */
                        AtCar dataDbResult = this.carService.findOne(paramQueryTemp);
                        if (dataDbResult != null) {
                            boolean dbFlag = false;
                            if (BasePojoEnum.OPERTYPE_DELETE.getCode().equalsIgnoreCase(operType)) {
                                /* 删除 */
                                dbFlag = this.carService.removeById(idTemp);
                            } else if (BasePojoEnum.OPERTYPE_RESTORE.getCode().equalsIgnoreCase(operType)) {
                                /* 还原,为什么这里面要传这么多参数,一切的一切就是为了在xml中拼装sql语句 */
                                paramQueryTemp = new AtCarQuery();
                                entity = paramQueryTemp.getEntity();
                                entity.setDelFlag(BasePojoEnum.DELFLAG_NO.getCode());
                                entity.setId(idTemp);
                                paramQueryTemp.setOperType(BasePojoEnum.OPERTYPE_RESTORE.getCode());
                                dbFlag = this.carService.updateBatch(paramQueryTemp) > 0 ;
                            } else if (BasePojoEnum.OPERTYPE_EMPTY.getCode().equalsIgnoreCase(operType)) {
                                /* 清空 */
                                paramQueryTemp = new AtCarQuery();
                                entity = paramQueryTemp.getEntity();
                                entity.setId(idTemp);
                                dbFlag = this.carService.deleteBatch(paramQueryTemp) > 0 ;
                            } else if (BasePojoEnum.OPERTYPE_UPDATESTATUS.getCode().equalsIgnoreCase(operType)) {
                                /* 批量修改状态 */
                                dataDbResult.setStatus(paramQuery.getEntity().getStatus());
                                dbFlag = this.carService.updateById(dataDbResult);
                            }
                            if (dbFlag) {
                                succedCount ++;
                            } else {
                                failedCount ++;
                            }
                        }
                    } catch (Exception e) {
                        log.error("批量操作失败了,id:{}", idTemp, e);
                        failedCount ++;
                    }
                }
            } else {
                /* 未选择类型,ids */
                info = "ids为空";
            }
        } else {
            /* 操作类型未选择 */
            info = "操作类型未选择";
        }

        String messInfo = "批量操作日志:提示信息:${info}, 总条数:${totalCount},成功条数:${succedCount},失败条数:${failedCount}";
        Map<String,String> paramsMap = new HashMap<>(1);
        paramsMap.put("info", info);
        paramsMap.put("totalCount", String.valueOf(totalCount));
        paramsMap.put("succedCount", String.valueOf(succedCount));
        paramsMap.put("failedCount", String.valueOf(failedCount));
        messInfo = this.regexUtil.replaceOperator(messInfo, paramsMap);

        paramsMap = new HashMap<>(1);
        paramsMap.put(ConstatFinalUtil.FINAL_CONFIG_INFO_SUCCESS_PARAM1, messInfo);
        /* 查询数据 */
        JSONObject resultJson = this.proccedResponseJson(ConstatFinalUtil.FINAL_OPER_SUCCESS.equalsIgnoreCase(info), paramsMap);
        log.info("==结果:{}==", resultJson);
        return resultJson.toJSONString();
    }


    /**
     * 路线查询多条
     * @return
     */
    @RequestMapping("/routeList")
    public String routeList(AtRouteQuery paramQuery, Model model){
        log.info("==路线列表==");
        /* 分页对象 */
        IPage<AtRoute> paramPage = null;
        try {
            paramPage = new Page<>(paramQuery.getCurrent(), paramQuery.getSize());
        } catch (Exception e) {
            paramPage = new Page<>(1, 20);
        }
        /* 时间处理 */
        if (StringUtils.isNotEmpty(paramQuery.getStDate())) {
            paramQuery.setPubTimeSt(this.dateUtil.strToDateTime(paramQuery.getStDate()));
        }
        if (StringUtils.isNotEmpty(paramQuery.getEdDate())) {
            paramQuery.setPubTimeEd(this.dateUtil.strToDateTime(paramQuery.getEdDate()));
        }
        paramQuery.addLike();
        /* 查询对象 */
        List<AtRoute> dataList = this.routeService.findList(paramPage, paramQuery);
        paramQuery.removeLike();
        /* 列表数据 */
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_DATALIST, dataList);
        /* 分页数据 */
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PAGEINFOUTIL, paramPage);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PARAMQUERY, paramQuery);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ENUMSONE, new AtRoute());

        /* 查询所有车辆数据 */
        AtCarQuery carQuery = new AtCarQuery();
        AtCar carEntity = carQuery.getEntity();
        carEntity.setDelFlag(BasePojoEnum.DELFLAG_NO.getCode());
        carEntity.setStatus(BasePojoEnum.STATUS_ENABLE.getCode());
        List<AtCar> carList = this.carService.findList(null, carQuery);
        model.addAttribute("carList", carList);
        /* 查询所有交通灯数据 */
        AtIntersectionQuery interQuery = new AtIntersectionQuery();
        AtIntersection interEntity = interQuery.getEntity();
        interEntity.setDelFlag(BasePojoEnum.DELFLAG_NO.getCode());
        interEntity.setStatus(BasePojoEnum.STATUS_ENABLE.getCode());
        List<AtIntersection> intersectionList = this.intersectionService.findList(null, interQuery);
        model.addAttribute("intersectionList", intersectionList);

        /* 查询数据 */
        return "/back/traffic/routeList";
    }

    /**
     * 路线打开更新页面
     * 更新和详情是同一个页面;
     * @return
     */
    @RequestMapping("/routeUpdate")
    public String routeUpdate(AtRouteQuery paramQuery, Model model){
        log.info("==路线更新==");
        /* 根据id查询 */
        AtRoute one = this.routeService.findOne(paramQuery);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ONE, one);

        if (BaseQuery.FINAL_OPER_UPDATE.equalsIgnoreCase(paramQuery.getOperType())) {
            /* 查询所有车辆数据 */
            AtCarQuery carQuery = new AtCarQuery();
            AtCar carEntity = carQuery.getEntity();
            carEntity.setDelFlag(BasePojoEnum.DELFLAG_NO.getCode());
            carEntity.setStatus(BasePojoEnum.STATUS_ENABLE.getCode());
            List<AtCar> carList = this.carService.findList(null, carQuery);
            model.addAttribute("carList", carList);
            /* 查询所有交通灯数据 */
            AtIntersectionQuery interQuery = new AtIntersectionQuery();
            AtIntersection interEntity = interQuery.getEntity();
            interEntity.setDelFlag(BasePojoEnum.DELFLAG_NO.getCode());
            interEntity.setStatus(BasePojoEnum.STATUS_ENABLE.getCode());
            List<AtIntersection> intersectionList = this.intersectionService.findList(null, interQuery);
            model.addAttribute("intersectionList", intersectionList);
            /* 查询数据 */
            return "/back/traffic/routeUpdate";
        }
        /* 查询数据 */
        return "/back/traffic/routeInfo";
    }

    /**
     * 路线更新提交操作
     * <pre>
     * 路线表有10个字段,用户填写的只有5个字段;
     * 更新的时候只更新5个字段;剩下的5个字段不用更新
     * 千万不要把页面中传的参数SpringMVC会自动创建对象,这个对象不要直接更新数据库,很危险;
     * </pre>
     * @return
     */
    @RequestMapping("/routeUpdateSubmit")
    @ResponseBody
    public String routeUpdateSubmit(AtRouteQuery paramQuery, Model model){
        log.info("==路线更新提交==");
        AtRoute entity = paramQuery.getEntity();
        if (StringUtils.isNotEmpty(paramQuery.getPubTime())) {
            entity.setPubTime(this.dateUtil.strToDateTime(paramQuery.getPubTime()));
        }
        boolean dbFlag = this.routeService.updateById(entity);
        JSONObject resultJson = this.proccedResponseJson(dbFlag);
        /* 返回json给客户端 */
        return resultJson.toJSONString();
    }

    /**
     * 路线打开添加页面
     * @return
     */
    @RequestMapping("/routeBatch")
    @ResponseBody
    public String routeBatch(AtRouteQuery paramQuery, Model model){
        log.info("==路线批量提交==");
        String operType = paramQuery.getOperType();
        String[] ids = paramQuery.getIds();
        String info = ConstatFinalUtil.FINAL_OPER_SUCCESS;
        int totalCount = 0;
        int succedCount = 0;
        int failedCount = 0;
        if (StringUtils.isNotEmpty(operType)) {
            if (Objects.nonNull(ids) && ids.length > 0) {
                totalCount = ids.length;
                for (String idTemp : ids) {
                    try {
                        AtRouteQuery paramQueryTemp = new AtRouteQuery();
                        AtRoute entity = paramQueryTemp.getEntity();
                        entity.setId(idTemp);
                        /* 根据id查询每一个 记录对象 */
                        AtRoute dataDbResult = this.routeService.findOne(paramQueryTemp);
                        if (dataDbResult != null) {
                            boolean dbFlag = false;
                            if (BasePojoEnum.OPERTYPE_DELETE.getCode().equalsIgnoreCase(operType)) {
                                /* 删除 */
                                dbFlag = this.routeService.removeById(idTemp);
                            } else if (BasePojoEnum.OPERTYPE_RESTORE.getCode().equalsIgnoreCase(operType)) {
                                /* 还原,为什么这里面要传这么多参数,一切的一切就是为了在xml中拼装sql语句 */
                                paramQueryTemp = new AtRouteQuery();
                                entity = paramQueryTemp.getEntity();
                                entity.setDelFlag(BasePojoEnum.DELFLAG_NO.getCode());
                                entity.setId(idTemp);
                                paramQueryTemp.setOperType(BasePojoEnum.OPERTYPE_RESTORE.getCode());
                                dbFlag = this.routeService.updateBatch(paramQueryTemp) > 0 ;
                            } else if (BasePojoEnum.OPERTYPE_EMPTY.getCode().equalsIgnoreCase(operType)) {
                                /* 清空 */
                                paramQueryTemp = new AtRouteQuery();
                                entity = paramQueryTemp.getEntity();
                                entity.setId(idTemp);
                                dbFlag = this.routeService.deleteBatch(paramQueryTemp) > 0 ;
                            } else if (BasePojoEnum.OPERTYPE_UPDATESTATUS.getCode().equalsIgnoreCase(operType)) {
                                /* 批量修改状态 */
                                dataDbResult.setStatus(paramQuery.getEntity().getStatus());
                                dbFlag = this.routeService.updateById(dataDbResult);
                            }
                            if (dbFlag) {
                                succedCount ++;
                            } else {
                                failedCount ++;
                            }
                        }
                    } catch (Exception e) {
                        log.error("批量操作失败了,id:{}", idTemp, e);
                        failedCount ++;
                    }
                }
            } else {
                /* 未选择类型,ids */
                info = "ids为空";
            }
        } else {
            /* 操作类型未选择 */
            info = "操作类型未选择";
        }

        String messInfo = "批量操作日志:提示信息:${info}, 总条数:${totalCount},成功条数:${succedCount},失败条数:${failedCount}";
        Map<String,String> paramsMap = new HashMap<>(1);
        paramsMap.put("info", info);
        paramsMap.put("totalCount", String.valueOf(totalCount));
        paramsMap.put("succedCount", String.valueOf(succedCount));
        paramsMap.put("failedCount", String.valueOf(failedCount));
        messInfo = this.regexUtil.replaceOperator(messInfo, paramsMap);

        paramsMap = new HashMap<>(1);
        paramsMap.put(ConstatFinalUtil.FINAL_CONFIG_INFO_SUCCESS_PARAM1, messInfo);
        /* 查询数据 */
        JSONObject resultJson = this.proccedResponseJson(ConstatFinalUtil.FINAL_OPER_SUCCESS.equalsIgnoreCase(info), paramsMap);
        log.info("==结果:{}==", resultJson);
        return resultJson.toJSONString();
    }

    /**
     * 统计查询多条
     * @return
     */
    @RequestMapping("/statList")
    public String statList(AtStatQuery paramQuery, Model model){
        log.info("==统计列表==");
        /* 分页对象 */
        IPage<AtStat> paramPage = null;
        try {
            paramPage = new Page<>(paramQuery.getCurrent(), paramQuery.getSize());
        } catch (Exception e) {
            paramPage = new Page<>(1, 20);
        }
        /* 时间处理 */
        if (StringUtils.isNotEmpty(paramQuery.getStDate())) {
            paramQuery.setPubTimeSt(this.dateUtil.strToDateTime(paramQuery.getStDate()));
        }
        if (StringUtils.isNotEmpty(paramQuery.getEdDate())) {
            paramQuery.setPubTimeEd(this.dateUtil.strToDateTime(paramQuery.getEdDate()));
        }
        paramQuery.addLike();
        /* 查询对象 */
        List<AtStat> dataList = this.statService.findList(paramPage, paramQuery);
        paramQuery.removeLike();
        /* 列表数据 */
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_DATALIST, dataList);
        /* 分页数据 */
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PAGEINFOUTIL, paramPage);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_PARAMQUERY, paramQuery);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ENUMSONE, new AtStat());

        /* 查询所有交通灯数据 */
        AtIntersectionQuery interQuery = new AtIntersectionQuery();
        AtIntersection interEntity = interQuery.getEntity();
        interEntity.setDelFlag(BasePojoEnum.DELFLAG_NO.getCode());
        interEntity.setStatus(BasePojoEnum.STATUS_ENABLE.getCode());
        List<AtIntersection> intersectionList = this.intersectionService.findList(null, interQuery);
        model.addAttribute("intersectionList", intersectionList);

        /* 查询数据 */
        return "/back/traffic/statList";
    }

    /**
     * 统计打开更新页面
     * 更新和详情是同一个页面;
     * @return
     */
    @RequestMapping("/statUpdate")
    public String statUpdate(AtStatQuery paramQuery, Model model){
        log.info("==统计更新==");
        /* 根据id查询 */
        AtStat one = this.statService.findOne(paramQuery);
        model.addAttribute(ConstatFinalUtil.FINAL_CODE_ONE, one);

        if (BaseQuery.FINAL_OPER_UPDATE.equalsIgnoreCase(paramQuery.getOperType())) {
            /* 查询所有车辆数据 */
            AtCarQuery carQuery = new AtCarQuery();
            AtCar carEntity = carQuery.getEntity();
            carEntity.setDelFlag(BasePojoEnum.DELFLAG_NO.getCode());
            carEntity.setStatus(BasePojoEnum.STATUS_ENABLE.getCode());
            List<AtCar> carList = this.carService.findList(null, carQuery);
            model.addAttribute("carList", carList);
            /* 查询所有交通灯数据 */
            AtIntersectionQuery interQuery = new AtIntersectionQuery();
            AtIntersection interEntity = interQuery.getEntity();
            interEntity.setDelFlag(BasePojoEnum.DELFLAG_NO.getCode());
            interEntity.setStatus(BasePojoEnum.STATUS_ENABLE.getCode());
            List<AtIntersection> intersectionList = this.intersectionService.findList(null, interQuery);
            model.addAttribute("intersectionList", intersectionList);
            /* 查询数据 */
            return "/back/traffic/statUpdate";
        }
        /* 查询数据 */
        return "/back/traffic/statInfo";
    }

    /**
     * 统计更新提交操作
     * <pre>
     * 统计表有10个字段,用户填写的只有5个字段;
     * 更新的时候只更新5个字段;剩下的5个字段不用更新
     * 千万不要把页面中传的参数SpringMVC会自动创建对象,这个对象不要直接更新数据库,很危险;
     * </pre>
     * @return
     */
    @RequestMapping("/statUpdateSubmit")
    @ResponseBody
    public String statUpdateSubmit(AtStatQuery paramQuery, Model model){
        log.info("==统计更新提交==");
        AtStat entity = paramQuery.getEntity();
        if (StringUtils.isNotEmpty(paramQuery.getPubTime())) {
            entity.setPubTime(this.dateUtil.strToDateTime(paramQuery.getPubTime()));
        }
        boolean dbFlag = this.statService.updateById(entity);
        JSONObject resultJson = this.proccedResponseJson(dbFlag);
        /* 返回json给客户端 */
        return resultJson.toJSONString();
    }

    /**
     * 统计打开添加页面
     * @return
     */
    @RequestMapping("/statBatch")
    @ResponseBody
    public String statBatch(AtStatQuery paramQuery, Model model){
        log.info("==统计批量提交==");
        String operType = paramQuery.getOperType();
        String[] ids = paramQuery.getIds();
        String info = ConstatFinalUtil.FINAL_OPER_SUCCESS;
        int totalCount = 0;
        int succedCount = 0;
        int failedCount = 0;
        if (StringUtils.isNotEmpty(operType)) {
            if (Objects.nonNull(ids) && ids.length > 0) {
                totalCount = ids.length;
                for (String idTemp : ids) {
                    try {
                        AtStatQuery paramQueryTemp = new AtStatQuery();
                        AtStat entity = paramQueryTemp.getEntity();
                        entity.setId(idTemp);
                        /* 根据id查询每一个 记录对象 */
                        AtStat dataDbResult = this.statService.findOne(paramQueryTemp);
                        if (dataDbResult != null) {
                            boolean dbFlag = false;
                            if (BasePojoEnum.OPERTYPE_DELETE.getCode().equalsIgnoreCase(operType)) {
                                /* 删除 */
                                dbFlag = this.statService.removeById(idTemp);
                            } else if (BasePojoEnum.OPERTYPE_RESTORE.getCode().equalsIgnoreCase(operType)) {
                                /* 还原,为什么这里面要传这么多参数,一切的一切就是为了在xml中拼装sql语句 */
                                paramQueryTemp = new AtStatQuery();
                                entity = paramQueryTemp.getEntity();
                                entity.setDelFlag(BasePojoEnum.DELFLAG_NO.getCode());
                                entity.setId(idTemp);
                                paramQueryTemp.setOperType(BasePojoEnum.OPERTYPE_RESTORE.getCode());
                                dbFlag = this.statService.updateBatch(paramQueryTemp) > 0 ;
                            } else if (BasePojoEnum.OPERTYPE_EMPTY.getCode().equalsIgnoreCase(operType)) {
                                /* 清空 */
                                paramQueryTemp = new AtStatQuery();
                                entity = paramQueryTemp.getEntity();
                                entity.setId(idTemp);
                                dbFlag = this.statService.deleteBatch(paramQueryTemp) > 0 ;
                            } else if (BasePojoEnum.OPERTYPE_UPDATESTATUS.getCode().equalsIgnoreCase(operType)) {
                                /* 批量修改状态 */
                                dataDbResult.setStatus(paramQuery.getEntity().getStatus());
                                dbFlag = this.statService.updateById(dataDbResult);
                            }
                            if (dbFlag) {
                                succedCount ++;
                            } else {
                                failedCount ++;
                            }
                        }
                    } catch (Exception e) {
                        log.error("批量操作失败了,id:{}", idTemp, e);
                        failedCount ++;
                    }
                }
            } else {
                /* 未选择类型,ids */
                info = "ids为空";
            }
        } else {
            /* 操作类型未选择 */
            info = "操作类型未选择";
        }

        String messInfo = "批量操作日志:提示信息:${info}, 总条数:${totalCount},成功条数:${succedCount},失败条数:${failedCount}";
        Map<String,String> paramsMap = new HashMap<>(1);
        paramsMap.put("info", info);
        paramsMap.put("totalCount", String.valueOf(totalCount));
        paramsMap.put("succedCount", String.valueOf(succedCount));
        paramsMap.put("failedCount", String.valueOf(failedCount));
        messInfo = this.regexUtil.replaceOperator(messInfo, paramsMap);

        paramsMap = new HashMap<>(1);
        paramsMap.put(ConstatFinalUtil.FINAL_CONFIG_INFO_SUCCESS_PARAM1, messInfo);
        /* 查询数据 */
        JSONObject resultJson = this.proccedResponseJson(ConstatFinalUtil.FINAL_OPER_SUCCESS.equalsIgnoreCase(info), paramsMap);
        log.info("==结果:{}==", resultJson);
        return resultJson.toJSONString();
    }
}
